using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;
using LMD.Lib.Oracle;

public partial class FeedSource : ICPAdminPage
{
    public DataTable m_dtSource = new DataTable();

    #region Properties
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Pengaturan Feed Source";
        if (!IsPostBack && !IsCallback)
        {
        }

        InitGrid(false);
    }
    protected void cbkEditor_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = Handle_Editor(e.Parameter);
    }
    protected void dgMain_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        InitGrid(true);
    }
    #endregion Event Handlers

    #region Custom Methods
    private void InitGrid(bool bRefresh)
    {
        Cached.FeedSource ccTmp = new Cached.FeedSource();
        m_dtSource = ccTmp.GetFeedSourceList(bRefresh);

        dgMain.Caption = "ICP Feed Source List";

        dgMain.DataSource = m_dtSource;
        dgMain.DataBind();
    }

    private String Handle_Editor(String sxParameter)
    {
        String sxReturn = String.Empty;
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        switch (sxSplitted[0].ToUpper())
        {
            case "ADD":
                DBQuery.FeedSourceTable.SaveFeedSource(sxSplitted[1], sxSplitted[2]);
                break;
            case "DEL":
                int iInputId = int.Parse(sxSplitted[1]);
                DBQuery.FeedSourceTable.DeleteFeedSource(iInputId);
                break;
            case "UPDATE":
                iInputId = int.Parse(sxSplitted[1]);
                DBQuery.FeedSourceTable.UpdateFeedSource(iInputId, sxSplitted[2], sxSplitted[3]);
                break;
            case "TOGGLE":
                iInputId = int.Parse(sxSplitted[1]);
                DBQuery.FeedSourceTable.ToggleFeedSource(iInputId);
                break;
            case "EDIT":
                iInputId = int.Parse(sxSplitted[1]);
                sxReturn = Build_Editor(iInputId);
                break;
        }
        return sxReturn;
    }

    private String Build_Editor(int iFeedSourceId)
    {
        String sxReturn = String.Empty;

        Cached.FeedSource ccTmp = new Cached.FeedSource();
        DataTable dtTmp = new DataView(
            ccTmp.GetFeedSourceList(true),
            "ID = " + iFeedSourceId.ToString(),
            String.Empty, DataViewRowState.CurrentRows).ToTable();

        if (dtTmp.Rows.Count > 0)
        {
            DataRow dr = dtTmp.Rows[0];
            sxReturn = AdminPageManager.BuildEditor_FeedSourcePopup(iFeedSourceId, dr["NAME"].ToString(),
                dr["URL"].ToString());
        }
        return sxReturn;
    }

    public String GetIcon(String sxActive)
    {
        return sxActive.ToUpper() == "Y" ? String.Empty : "0";
    }

    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    #endregion Custom Methods


}
